package org.authsum.stitches.wicket.common;



import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.Model;



public abstract class PageableTable extends Panel {

	
	private static Log logger = LogFactory.getLog(PageableTable.class);

	
	
	//protected DmiFeedbackPanel feedbackPanel = null;

	AjaxPagingNavigator topApn = null;
	
	AjaxPagingNavigator bottomApn = null;
	
	protected List docs = null;
	protected PageableListView listview = null;
	protected int pageCount = 10;

	AjaxLink make10 = null;
	AjaxLink make20 = null;
	AjaxLink make50 = null;
	//we keep this around in case user changes paging
	//private DMIResult dmiResult = null;
	
	private void fixLinks() {
		make10.setEnabled(pageCount != 10);
		make20.setEnabled(pageCount != 20);
		make50.setEnabled(pageCount != 50);
		
	}
	public abstract Panel getActionPanel(ListItem item);
	
	//protected AttractionSearchResultsListener attractionSearchRL = null;
	
	
	private void addPagingLink(String id,final PageableTable pt,final int pageCount) {
		AjaxLink ajaxLink = (new AjaxLink(id) {
				private static final long serialVersionUID = 1L;

				public void onClick(AjaxRequestTarget target) {
					logger.debug("  onClick" );
					//pageCount =10;
					listview.setRowsPerPage(pageCount);
					listview.setCurrentPage(0);
					target.addComponent(pt);
					//attractionSearchRL.searchComplete(target,dmiResult);

					
				}
			});
		 add(ajaxLink);
			Model model2 = new Model() {
				public Serializable getObject() {
					return "Display " + pageCount + "  results per page";
				}
			};
			
		 
		 //Display 10 results per page
		 ajaxLink.add(new AttributeModifier("title", true, model2));
		    
			
			
			
		// return ajaxLink;
	}
	
	public PageableTable(String id,List docs) {
		
		super(id);
		//this.attractionSearchRL = asrl;
		//this.feedbackPanel = feedbackPanel;
		this.setOutputMarkupId(true);
		
		addPagingLink("make10",this,10);
		addPagingLink("make20",this,20);
		addPagingLink("make50",this,50);
		
		 


		
		this.docs = docs;


		
	}
	
	protected void postInit() {
		listview = getPageableListView();

		// http://wicketstuff.org/wicket13doc/org/apache/wicket/markup/html/list/PageableListView.html
		add(listview);
		topApn = new AjaxPagingNavigator("topApn",	listview);
		add(topApn);
		bottomApn = new AjaxPagingNavigator("bottomApn",	listview);
		add(bottomApn);
		
		

		// 1 - 10 of 876 results
		// int cp = listview.getCurrentPage();
		Model resultsCountModel = new Model() {
			public Serializable getObject() {
				return "Showing " + getFrom() + " to " + getTo() + " of "
						+ getOf() + " results";

			}
		};

		Label resultsCountLabel = new Label("resultsCountLabel",
				resultsCountModel);

		add(resultsCountLabel);

		// NavigatorLabel navigatorLabel = new
		// NavigatorLabel("navigatorLabel",listview);

		setVisible(!docs.isEmpty());
		
		topApn.setVisible(docs.size() >= pageCount);
		
		bottomApn.setVisible(docs.size() >= pageCount);
		
	}

	protected abstract PageableListView getPageableListView();
		
		
	

	
	/**
	 * @return "z" in "Showing x to y of z"
	 */
	public int getOf() {
		return docs.size();
	}

	/**
	 * @return "x" in "Showing x to y of z"
	 */
	public int getFrom() {
		if (getOf() == 0) {
			return 0;
		}
		return (listview.getCurrentPage() * listview.getRowsPerPage()) + 1;
	}

	/**
	 * @return "y" in "Showing x to y of z"
	 */
	public int getTo() {
		if (getOf() == 0) {
			return 0;
		}
		return Math.min(getOf(), getFrom() + listview.getRowsPerPage() - 1);
	}
	
	
}
